

package com.hazelcast.internal.util.comparators;

import com.hazelcast.internal.serialization.Data;
import com.hazelcast.internal.serialization.SerializationService;

import java.util.Objects;

/**
 * Comparator for {@link com.hazelcast.config.InMemoryFormat#OBJECT}
 * backed data structures.
 */
final class ObjectValueComparator implements ValueComparator {

    public static final ValueComparator INSTANCE = new ObjectValueComparator();

    private ObjectValueComparator() {
    }

    @Override
    public boolean isEqual(Object value1, Object value2, SerializationService ss) {
        if (value1 == value2) {
            return true;
        }
        if (value1 == null || value2 == null) {
            return false;
        }
        Object v1 = value1 instanceof Data ? ss.toObject(value1) : value1;
        Object v2 = value2 instanceof Data ? ss.toObject(value2) : value2;
        return Objects.equals(v1, v2);
    }
}
